package ad.android.action;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import ad.android.action.base.BaseAction;
import ad.android.dto.AdDto;
import ad.android.dto.RequestAdJsonDto;
import ad.android.exception.ServiceException;
import ad.android.service.IAdAppStatisticsService;
import ad.android.service.IAdService;
import ad.android.service.IApplicationService;
import ad.android.util.Base64;
import ad.android.util.Constants;
import ad.android.util.DESedeCoder;
import ad.android.util.FileReaderUtil;
import ad.android.util.JsonToDtoUtil;

public class AdAction extends BaseAction implements /*ServletResponseAware,*/ServletRequestAware{
	private AdDto adDto;
	/*private HttpServletResponse servletResponse;*/
	private HttpServletRequest servletRequest;
	private IAdService adService;
	private IApplicationService appService;
	private IAdAppStatisticsService adAppStatisticsService;
	
	public String getAd(){
		try {
			logDebug("getAd is called.");
			adDto = new AdDto();
			/*servletResponse.setHeader("hello-world", "yes");*/
			
			//获取请求中的数据并用Base64解码
			byte[] data = Base64.decode( servletRequest.getParameter("data") );
			logDebug(">>data:" + servletRequest.getParameter("data"));
			logDebug( "接收到的数据:" + new String(data,"UTF-8"));
			
			//获取licenseKey
			String licenseKey = servletRequest.getHeader("licenseKey");
			if( licenseKey == null){
				log.info("get licenseKey error.");
				return ERROR;
			}
			/*String secKey = "a61A+5uKrsDvf3DuIoLifhEx9ryIUDH5wqYdlut";*/
			//获取secKey
			String secKey = appService.findSeckeyByLicenseKey(licenseKey);
			
			//解密
			String decryptData = new String(DESedeCoder.decrypt(data, secKey.getBytes()));
			logDebug( "解密后的数据:" + decryptData);
			
			//将Json转换为Dto对象
			RequestAdJsonDto requestAdJsonDto = JsonToDtoUtil.transToDto(decryptData, RequestAdJsonDto.class);
			
			if( requestAdJsonDto == null){
				log.info("convert requestAdJsonDto error.");
				return ERROR;
			}
			
			//FIXME: 测试数据
//			requestAdJsonDto = new RequestAdJsonDto();
//			requestAdJsonDto.setLicensekey("license_key_1");
			//FIXME: 测试数据
			
			//获取广告Dto
			adDto = adService.findAd(requestAdJsonDto);
			
			if( adDto == null ){
				log.error("adDto is null.");
				throw new ServiceException("adDto is null.");
			}
			
			Long appId = appService.findApplicationIdByLicenseKey(licenseKey);
			adAppStatisticsService.addDownloadNum(appId, Long.parseLong(adDto.getAdId()),Constants.NUM_1);
			
			return SUCCESS;
		} catch (Exception e) {
			log.debug("getAd", e);
			return SUCCESS;
		}
	}

	public String test(){
		try {
			/*String html = FileReaderUtil.readTmplHtml("tmpl110914.html");
			logDebug("HTML:" + html);*/
			RequestAdJsonDto requestAdJsonDto = new RequestAdJsonDto();
			requestAdJsonDto.setLicensekey("license_key_1");
			adDto = adService.findAd(requestAdJsonDto);
			log.debug("AAAAAAAAAAAAAAAAAAAAAA:"+ adDto);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return SUCCESS;
		}
	}
	
	/**
	 * 广告被点击
	 * 参数：licenseKey 和 adId
	 * @return
	 */
	public String adClick(){
		try {
			//get license key of app
			String licenseKey = servletRequest.getHeader("licenseKey");
//			licenseKey = "license_key_1";
			if( licenseKey == null){
				log.info("get licenseKey error.");
				return ERROR;
			}
			
			if( log.isDebugEnabled() ){
				log.debug("adClick->licenseKey:" + licenseKey);
			}
			Long appId = appService.findApplicationIdByLicenseKey(licenseKey);
			
			if( log.isDebugEnabled() ){
				log.debug("adClick->appId:" + appId);
			}
			
			Long adId = Long.parseLong( servletRequest.getHeader("adId") );
			if( adId == null ){
				log.error("adId can not be null.");
				throw new Exception("adId can not be null.");
			}
			if( log.isDebugEnabled() ){
				log.debug("adClick->adId:" + adId);
			}
//			Long adId = 1L;
			adAppStatisticsService.addClickNum(appId,adId,Constants.NUM_1);
			
			return SUCCESS;
		} catch (Exception e) {
			log.error("adClick",e);
			return ERROR;
		}
	}
	
	
	public AdDto getAdDto() {
		return adDto;
	}

	public void setAdDto(AdDto adDto) {
		this.adDto = adDto;
	}

	/*public HttpServletResponse getServletResponse() {
		return servletResponse;
	}

	public void setServletResponse(HttpServletResponse servletResponse) {
		this.servletResponse = servletResponse;
	}*/

	public HttpServletRequest getServletRequest() {
		return servletRequest;
	}

	public void setServletRequest(HttpServletRequest servletRequest) {
		this.servletRequest = servletRequest;
	}

	public IAdService getAdService() {
		return adService;
	}

	public void setAdService(IAdService adService) {
		this.adService = adService;
	}

	public IApplicationService getAppService() {
		return appService;
	}

	public void setAppService(IApplicationService appService) {
		this.appService = appService;
	}

	public IAdAppStatisticsService getAdAppStatisticsService() {
		return adAppStatisticsService;
	}

	public void setAdAppStatisticsService(
			IAdAppStatisticsService adAppStatisticsService) {
		this.adAppStatisticsService = adAppStatisticsService;
	}
	
}
